package filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.FoodType;
import dto.MItem;
import service.FoodListManager;
import service.MListManager;
import service.factory.FoodListManagerFactory;
import service.factory.MListManagerFactory;

/**
 * Servlet Filter implementation class SecFilter
 */
public class SecFilter implements Filter {
    MListManager manager;
    /**
     * Default constructor. 
     */
    public SecFilter() {
        manager = new MListManagerFactory().getInstance();
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
        List<MItem> mlist = manager.getMList();
//        
        FoodListManager manager = FoodListManagerFactory.getInstance();
        List<FoodType> foodType = manager.loadFoodType();
        
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        
        req.getSession().setAttribute("foodtypes", foodType);
       if(mlist == null){
    	   System.out.println("mlist为null!!!!!!");
    	   resp.sendRedirect("/ceat/error.html");
       }
       else{
    	   System.out.println("mlist不为null");
    	   req.getSession().setAttribute("mlist", mlist);
    	   chain.doFilter(request, response);
       }
       
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
